/*
 * CrashUtils.java
 * business
 *
 * Created by ChenTao on 2017/1/3.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.utils;

import android.content.Context;
import android.os.Environment;
import android.webkit.WebView;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.tinker.TinkerUtils;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.tinker.loader.app.ApplicationLike;
import com.yidiandao.business.BuildConfig;
import com.yidiandao.business.R;
import com.yidiandao.business.ui.main.MainActivity;
import com.yidiandao.business.ui.userabout.setting.SettingActivity;

/**
 * Bugly相关工具
 * <p>
 * <p>Bugly中包含了CrashReport、Upgrade、HotFix功能
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/11 14:57
 */
public class BuglyUtils {

    private static final String BUGLY_APP_ID = "6724557d26";

    public static void installTinker(ApplicationLike applicationLike) {
        Beta.installTinker(applicationLike);
    }

    /**
     * 初始化Bugly
     */
    public static void initBugly(Context context) {
        //Bugly CrashReport Update 统一初始化方法：
        Bugly.init(context, BUGLY_APP_ID, false);
    }

    /**
     * 初始化更新策略
     */
    public static void initUpdateStrategy() {
        // 自动初始化开关
        Beta.autoInit = true;
        // 自动检测更新开关
        Beta.autoCheckUpgrade = true;
        // 设置升级检测周期为：60秒
        Beta.upgradeCheckPeriod = 60 * 1000;
        // 延迟初始化
        Beta.initDelay = 2 * 1000;
        // 设置通知栏图标
        Beta.largeIconId = R.mipmap.logo;
        Beta.smallIconId = R.mipmap.logo;
        // 设置更新弹窗默认展示的banner
        Beta.defaultBannerId = R.mipmap.ic_image_normal_2_66;
        // 设置sd卡的Download为更新资源存储目录
        Beta.storageDir =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        // 设置开启显示打断策略：点击过确认的弹窗再App下次启动自动检查更新时会再次显示
        Beta.showInterruptedStrategy = true;
        // 添加可以显示弹窗的Activity
        Beta.canShowUpgradeActs.add(MainActivity.class);
        Beta.canShowUpgradeActs.add(SettingActivity.class);
        // 设置自定义升级对话框UI布局.
        Beta.upgradeDialogLayoutId = R.layout.dialog_upgrade;
        // 设置自定义tip弹窗UI布局
        Beta.tipsDialogLayoutId = R.layout.dialog_upgrade_tips;
        // 设置是否显示弹窗中的apk信息
        Beta.canShowApkInfo = true;
        // 设置Wifi下自动下载
        Beta.autoDownloadOnWifi = false;
    }

    public static void setAppBackground(boolean background) {
        TinkerUtils.setBackground(background);
    }

    public static void setUserId(String userId) {
        CrashReport.setUserId(userId);
    }

    /**
     * 检测更新
     */
    public static void checkUpdate() {
        Beta.checkUpgrade();
    }

    /**
     * 上传自定义错误到Bugly
     */
    public static void reportError(Throwable throwable) {
        // bugly会将这个throwable上报
        CrashReport.postCatchedException(throwable);
    }

    /**
     * 设置是否为开发设备
     */
    public static void setIsDevelopmentDevice(Context context) {
        CrashReport.setIsDevelopmentDevice(context, BuildConfig.DEBUG);
    }

    /**
     * 上传JS错误到Bugly
     */
    public static void setJavascriptMonitor(WebView webView) {
        CrashReport.setJavascriptMonitor(webView, true);
    }
}
